package com.example.auth.service;

import com.example.auth.domain.DataNode;
import com.example.auth.domain.OauthUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

import java.util.List;

/**
 * @Classname MyUserDetailService
 * @Description TODO
 * @Date 2022/3/22 13:49
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class OaUserDetailService implements UserDetailsService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserService userService;

    @Autowired
    private DataNodeService dataNodeConfigService;

    @Override
    public UserDetails loadUserByUsername(String username) {
        logger.info("【数据库】查询登录用户名:" + username);
        com.example.auth.domain.User user=userService.getUserByHashUserName(username);
        if(user==null){
            return null;
        }
        DataNode nodeConfig=userService.findDataNodeByUser(user);

        //根据查找到的用户信息判断用户是否被冻结
        OauthUser oauthUser= new OauthUser(user.getUuid(),user.getUsername(),user.getPassword(),AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_USER"));
        if(nodeConfig!=null){
            oauthUser.setNodeId(user.getNodeId());
            oauthUser.setNodeName(nodeConfig.getNodeServerName());
        }
        return oauthUser;
        //        return new User(user.getUsername(), user.getPassword(),
//                true, true, true, true,
//                AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_USER"));
    }
}
